package com.diandiansong.app.ui.commonlist;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.AdapterView;

import com.diandiansong.app.App;
import com.diandiansong.app.R;
import com.diandiansong.app.entity.CommodityInfoInfo;
import com.diandiansong.app.ui.base.BaseAct;
import com.diandiansong.app.ui.index.GoodDetail;
import com.diandiansong.app.utils.A;
import com.diandiansong.app.utils.Api;
import com.diandiansong.app.widgets.AddAndMinusViewInItem;

import java.util.List;

import diandiansong.library.ui.base.FastListFragment;
import diandiansong.library.utils.CCallBack;

/**
 * 通用商品列表
 */
public class CommonList extends BaseAct {
    public static void start(Context context) {
        Intent starter = new Intent(context, CommonList.class);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initTitle("常用清单");
        addFragment(new SubFragment());
    }


    public static class SubFragment extends FastListFragment {

        @Override
        public int getItemLayoutRes() {
            return R.layout.good_item_add_minus;
        }

        @Override
        public void bindItemView(int position, @NonNull View convertView) {
            App.initGoodList(getActivity(), (CommodityInfoInfo.Data) getItem(position), position, convertView);
        }

        /**
         * 滑到底部时调用
         */
        @Override
        protected void loadMore() {
            A.apiMyCommon_list(mPage, new CCallBack<CommodityInfoInfo>() {
                @Override
                public void onSuccess(CommodityInfoInfo o) throws Exception {
                    List<CommodityInfoInfo.Data> data = o.getData();
                    if (mPage.equals("0")) {
                        mAdapter.setData(data);
                    } else {
                        mAdapter.addAll(data);
                    }
                    if (mAdapter.getDataSize() > 0) {
                        mPage = ((CommodityInfoInfo.Data) getLast()).getCommodityId();
                    }
                    onLoadFinishHasMore(data.size() >= Api.PAGE_SIZE);
                }

                @Override
                public void onFailure(int code, String msg) {
                    onLoadFinishNoMore();
                    toast(msg);
                }
            });
        }

        @Override
        public void onItemClick(AdapterView parent, View view, int position, long id) {
            AddAndMinusViewInItem aamvii = view.findViewById(R.id.add_minus);
            CommodityInfoInfo.Data item = (CommodityInfoInfo.Data) getItem(position);
            GoodDetail.start(getActivity(), item.getCommodityId());
        }
    }
}